package com.localjoycommunity.cpdj.project.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.common.utils.DateUtils;
import com.localjoycommunity.cpdj.project.system.domain.SysPaper;
import com.localjoycommunity.cpdj.project.system.domain.SysPaperQuestion;
import com.localjoycommunity.cpdj.project.system.mapper.SysPaperMapper;
import com.localjoycommunity.cpdj.project.system.mapper.SysPaperQuestionMapper;
import com.localjoycommunity.cpdj.project.system.mapper.SysQuestionMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserAnswerMapper;
import com.localjoycommunity.cpdj.project.system.service.ISysPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 试卷Service业务层处理
 *
 * @author leepandar
 * @date 2020-11-01
 */
@Service
public class SysPaperServiceImpl extends ServiceImpl<SysPaperMapper, SysPaper> implements ISysPaperService {
    @Autowired
    private SysPaperMapper sysPaperMapper;

    @Autowired
    private UserAnswerMapper userAnswerMapper;

    @Autowired
    private SysQuestionMapper sysQuestionMapper;

    @Autowired
    private SysPaperQuestionMapper paperQuestionMapper;

    /**
     * 查询试卷
     *
     * @param paperId 试卷ID
     * @return 试卷
     */
    @Override
    public SysPaper selectSysPaperById(Long paperId) {
        SysPaper sysPaper = sysPaperMapper.selectSysPaperById(paperId);
//        UserAnswer userAnswer = new UserAnswer();
//        userAnswer.setPaperId(paperId);
//        List<UserAnswer> list = userAnswerMapper.selectUserAnswerList(userAnswer);
//        if (list != null && list.size() > 0) {
//            sysPaper.setAnswerNum(list.size());
//        } else {
//            sysPaper.setAnswerNum(0);
//        }
//        List<SysPaperQuestion> sysPaperQuestions = this.sysPaperMapper.selectPaperQuestion(paperId);
//        if (sysPaperQuestions != null && sysPaperQuestions.size() > 0) {
//            sysPaper.setQuestionNum(sysPaperQuestions.size());
//            sysPaper.setPaperQuestions(sysPaperQuestions);
//            for (SysPaperQuestion paperQuestion : sysPaperQuestions) {
//                SysQuestion sysQuestion = this.sysQuestionMapper.selectSysQuestionById(paperQuestion.getQuestionId());
//                paperQuestion.setSysQuestion(sysQuestion);
//            }
//        } else {
//            sysPaper.setQuestionNum(0);
//        }
//        sysPaper.setPaperQuestions(sysPaperQuestions);
        return sysPaper;
    }

    /**
     * 查询试卷列表
     *
     * @param sysPaper 试卷
     * @return 试卷
     */
    @Override
    public List<SysPaper> selectSysPaperList(SysPaper sysPaper) {
        List<SysPaper> sysPaperList = sysPaperMapper.selectSysPaperList(sysPaper);
        sysPaperList.forEach(paper -> {
            List<SysPaperQuestion> paperQuestionList =
                    paperQuestionMapper.selectSysPaperQuestionByPaperId(paper.getPaperId());
            if (paperQuestionList != null && paperQuestionList.size() > 0) {
                paper.setQuestionNum(paperQuestionList.size());
            } else {
                paper.setQuestionNum(0);
            }
        });
        return sysPaperList;
    }

    /**
     * 新增试卷
     *
     * @param sysPaper 试卷
     * @return 结果
     */
    @Override
    public Long insertSysPaper(SysPaper sysPaper) {
        sysPaper.setCreateTime(DateUtils.getNowDate());
        sysPaper.setUpdateTime(DateUtils.getNowDate());
        List<SysPaperQuestion> paperQuestions = sysPaper.getPaperQuestions();
        if (paperQuestions != null && paperQuestions.size() > 0) {
            sysPaper.setQuestionNum(paperQuestions.size());
        } else {
            sysPaper.setQuestionNum(0);
        }
        sysPaperMapper.insert(sysPaper);
        return sysPaper.getPaperId();
    }

    /**
     * 修改试卷
     *
     * @param sysPaper 试卷
     * @return 结果
     */
    @Override
    public int updateSysPaper(SysPaper sysPaper) {
        sysPaper.setUpdateTime(DateUtils.getNowDate());
        List<SysPaperQuestion> paperQuestions = sysPaper.getPaperQuestions();
        if (paperQuestions != null && paperQuestions.size() > 0) {
            sysPaper.setQuestionNum(paperQuestions.size());
        }
        return sysPaperMapper.updateSysPaper(sysPaper);
    }

    /**
     * 批量删除试卷
     *
     * @param paperIds 需要删除的试卷ID
     * @return 结果
     */
    @Override
    public int deleteSysPaperByIds(Long[] paperIds) {
        return sysPaperMapper.deleteSysPaperByIds(paperIds);
    }

    /**
     * 删除试卷信息
     *
     * @param paperId 试卷ID
     * @return 结果
     */
    @Override
    public int deleteSysPaperById(Long paperId) {
        return sysPaperMapper.deleteSysPaperById(paperId);
    }
}
